﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Threading;

namespace THO7AlgoritmTimerApplication
{
    public static class Filters
    {
        public static Bitmap MedianFilter(Bitmap sourceImage, int Size)
        {
            Bitmap returnImage = new Bitmap(sourceImage);

            //copy to snell class for fast access
            snell srcImg = new snell(sourceImage);
            snell retImg = new snell(returnImage);

            //lock images for use
            srcImg.Lock();
            retImg.Lock();

            //define middle pixel
            int min = -(Size / 2);
            int max = (Size / 2);

            //
            for (int x = 0; x < retImg.Width; x++)
            {
                for (int y = 0; y < retImg.Height; y++)
                {
                    //make lists for RGB values
                    List<int> RValues = new List<int>();
                    List<int> GValues = new List<int>();
                    List<int> BValues = new List<int>();
                    for (int x2 = min; x2 < max; x2++)
                    {
                        int TempX = x + x2;
                        if (TempX >= 0 && TempX < retImg.Width)
                        {
                            for (int y2 = min; y2 < max; y2++)
                            {
                                int TempY = y + y2;
                                if (TempY >= 0 && TempY < retImg.Height)
                                {
                                    //store RGB values in lists
                                    Color color = srcImg.GetPixel(TempX, TempY);
                                    RValues.Add(color.R);
                                    GValues.Add(color.G);
                                    BValues.Add(color.B);
                                }
                            }
                        }
                    }
                    //sort all RGB values
                    RValues.Sort(); 
                    GValues.Sort();
                    BValues.Sort();
                    // take the median
                    Color MedianPixel = Color.FromArgb(RValues[RValues.Count / 2], GValues[GValues.Count / 2], BValues[BValues.Count / 2]);
                    //set the median
                    retImg.SetPixel(x, y, MedianPixel);
                }
            }

            //unlock and return image for showing
            srcImg.Unlock(true);
            retImg.Unlock(true);
            return returnImage;
        }

        public static Bitmap MedianFilterBogo(Bitmap sourceImage, int Size)
        {
            Bitmap returnImage = new Bitmap(sourceImage);

            //copy to snell class for fast access
            snell srcImg = new snell(sourceImage);
            snell retImg = new snell(returnImage);

            //lock images for use
            srcImg.Lock();
            retImg.Lock();

            //define middle pixel
            int min = -(Size / 2);
            int max = (Size / 2);

            //
            for (int x = 0; x < retImg.Width; x++)
            {
                for (int y = 0; y < retImg.Height; y++)
                {
                    //make lists for RGB values
                    List<int> RValues = new List<int>();
                    List<int> GValues = new List<int>();
                    List<int> BValues = new List<int>();
                    for (int x2 = min; x2 < max; x2++)
                    {
                        int TempX = x + x2;
                        if (TempX >= 0 && TempX < retImg.Width)
                        {
                            for (int y2 = min; y2 < max; y2++)
                            {
                                int TempY = y + y2;
                                if (TempY >= 0 && TempY < retImg.Height)
                                {
                                    //store RGB values in lists
                                    Color color = srcImg.GetPixel(TempX, TempY);
                                    RValues.Add(color.R);
                                    GValues.Add(color.G);
                                    BValues.Add(color.B);
                                }
                            }
                        }
                    }
                    //sort all RGB values
                    SortAlgorithms.bogosortList<int>(RValues);
                    SortAlgorithms.bogosortList<int>(GValues);
                    SortAlgorithms.bogosortList<int>(BValues);
                    // take the median
                    Color MedianPixel = Color.FromArgb(RValues[RValues.Count / 2], GValues[GValues.Count / 2], BValues[BValues.Count / 2]);
                    //set the median
                    retImg.SetPixel(x, y, MedianPixel);
                }
            }

            //unlock and return image for showing
            srcImg.Unlock(true);
            retImg.Unlock(true);
            return returnImage;
        }


    }
}
